import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_mmvm_provider/service/service.dart';
import 'package:flutter_mmvm_provider/viewmodel/joke_view_model.dart';
import 'package:flutter_mmvm_provider/widgets/joke_iitem.dart';
import 'package:provider/provider.dart';

class JokeView extends StatefulWidget {
  @override
  State<JokeView> createState() => _JokeViewState();
}

class _JokeViewState extends State<JokeView> {
  @override
  void initState() {
    // TODO: implement initState
    Service.getjokes(Provider.of<JokeViewModel>(context,listen: false)).then((value){

    }).catchError((error){
      print(error);
    });
    super.initState();
  }
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("provide+mvvm"),
      ),
      body: Consumer<JokeViewModel>(
        builder:(context,jokeviewmodel,child){
          JokeViewModel _jokeviewmodel = jokeviewmodel;
          if(_jokeviewmodel.loading){
            return Center(child: CircularProgressIndicator(),);
          }
         return ListView.separated(
              itemBuilder: (context,index){
                return JokeItem(index: index,joke: _jokeviewmodel.jokelist[index],);
              },
              separatorBuilder: (context,index){
                return Divider(height: 1,color: Colors.grey);
              },
              itemCount: _jokeviewmodel.jokelist.length);
        },
      ),
    );
  }
}
